package com.sunyur.tailor.channel.web.outer.budget.controller;

import com.alibaba.fastjson.JSON;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.service.inner.budget.param.BatchSyncCostCenterParam;
import com.sunyur.tailor.channel.service.inner.budget.service.SunyurBudgetService;
import com.sunyur.tailor.channel.web.outer.budget.dao.BatchSyncCostCenterDTO;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author pengbing
 * @Description //财务控制层
 * @Date 15:45 2021/6/1
 * @Param
 * @return
 **/
@Log4j2
@RestController
@RequestMapping("/outer/budget")
public class BudgetController {
    /**
     * 成本中心数据同步接口服务
     */
    @Autowired
    private SunyurBudgetService sunyurBudgetService;

    /**
     *成本中心数据同步
     */
    @PostMapping(value = "/batchCreateCostCenter", produces = MediaType.APPLICATION_JSON_VALUE)
    public ChannelResult batchCreateCostCenter(@RequestBody List<BatchSyncCostCenterDTO> batchSyncCostCenterDTOList) {
        log.info("BudgetController batchCreateCostCenter param:{}", JSON.toJSONString(batchSyncCostCenterDTOList));
        ChannelResult result = new ChannelResult<>();
        if (CollectionUtils.isEmpty(batchSyncCostCenterDTOList)) {
            log.error("BudgetController batchCreateCostCenter param is null");
            result.setError(MiddleResultEnum.PARAM_NULL.getCode(), MiddleResultEnum.PARAM_NULL.getValue());
            return result;
        }
        try {
            List<BatchSyncCostCenterParam> reqParamList = getBatchSyncCostCenterParams(batchSyncCostCenterDTOList);
            result = sunyurBudgetService.batchModifyCostCenter(reqParamList);
        } catch (Exception e) {
            log.error("BudgetController batchCreateCostCenter exception:{}", e);
            result.setError(MiddleResultEnum.EXCEPTION.getCode(), MiddleResultEnum.EXCEPTION.getValue());
        }
        return result;
    }

    private List<BatchSyncCostCenterParam> getBatchSyncCostCenterParams(List<BatchSyncCostCenterDTO> batchSyncCostCenterDTOList) {
        List<BatchSyncCostCenterParam> reqParamList=new ArrayList<>();
        for (BatchSyncCostCenterDTO batchSyncCostCenterDTO : batchSyncCostCenterDTOList) {
            BatchSyncCostCenterParam batchSyncCostCenterParam = new BatchSyncCostCenterParam();
            batchSyncCostCenterParam.setCostCenterOutSideCode(batchSyncCostCenterDTO.getCostCenterOutSideCode());
            batchSyncCostCenterParam.setCostCenterOutSideName(batchSyncCostCenterDTO.getCostCenterOutSideName());
            batchSyncCostCenterParam.setCompanyOutsideCodeList(batchSyncCostCenterDTO.getCompanyOutsideCodeList());
            batchSyncCostCenterParam.setCompanyOutsideCodeList(batchSyncCostCenterDTO.getBeneficiaryOutsideCodeList());
            reqParamList.add(batchSyncCostCenterParam);
        }
        return reqParamList;
    }
}
